/**
 * 
 */
package com.xinghou.platform.file.handler.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.chain.impl.ContextBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import com.google.gson.reflect.TypeToken;
import com.xinghou.platform.file.chain.impl.PhotoUploadChain;
import com.xinghou.platform.file.handler.BaseHandler;
import com.xinghou.platform.file.model.ResourceContext;
import com.xinghou.platform.file.service.UpphotoService;
import com.xinghou.platform.file.service.impl.UpphotoServiceImpl;
import com.xinghou.platform.util.ConfigUtil;
import com.xinghou.platform.util.JsonUtil;

/**
 * @author xuzhen
 *
 */
@Service
public class UpPhotoHandler implements BaseHandler {
	
	@Resource
	private PhotoUploadChain photoChain;
	
	@Resource
	private UpphotoService upphotoService;

	public void execute(ContextBase context) throws Exception {
		@SuppressWarnings("unchecked")
		ResourceContext<byte[], String> resourceContext = (ResourceContext<byte[], String>)context;
		resourceContext.getResponse().setResultCode(0);
		photoChain.execute(resourceContext);
		if(resourceContext.getResponse().getResultCode() == 0){
			Map<String, String> result = new HashMap<String, String>();
			result.put("result", "0");
			result.put("ckresult", "0");
			result.put("simphotoid", String.valueOf(Integer.valueOf(context.get("pid").toString())*10+1));
			String absSourcePath = resourceContext.getSource().getPath() 
					+ resourceContext.getSource().getSubPath().replace("\\", "/");
			String absThumbnailsPath =resourceContext.getExtars()[0].getPath() 
					+ resourceContext.getExtars()[0].getSubPath().replace("\\", "/");
			result.put("simphotoname",resourceContext.getExtars()[0].getResourceName());
			result.put("simphotourl", ConfigUtil.getProperty("local_domain") 
					//+"/head_img"
					+ absThumbnailsPath+"/"+"160"+resourceContext.getExtars()[0].getResourceName());
			result.put("photoid",  String.valueOf(Integer.valueOf(context.get("pid").toString())*10));
			result.put("photourl", ConfigUtil.getProperty("local_domain") 
					//+"/head_img"
					+ absSourcePath+"/"+resourceContext.getSource().getResourceName());
			result.put("photoname", resourceContext.getSource().getResourceName());
			result.put("errcode", "0");
			result.put("errmsg", "success");
			//result.put("pictureurl", resourceContext.getExtars()[0].getSubPath().replace("\\", "/"));
			
			resourceContext.getResponse().setBody(JsonUtil.toJson(result));
			resourceContext.getResponse().getResponseHeaders().set("ContentType", "application/json;charset=UTF-8");
			resourceContext.getResponse().setStatusCode(HttpStatus.OK);
		}else if(resourceContext.getResponse().getResultCode() > 0){
			resourceContext.getResponse().getResponseHeaders().set("ContentType", "application/json;charset=UTF-8");
			resourceContext.getResponse().setStatusCode(HttpStatus.OK);
		}else{
			resourceContext.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
		}
		
	}
	/**
	 * @param photoChain the photoChain to set
	 */
	public void setCrebitChain(PhotoUploadChain photoChain) {
		this.photoChain = photoChain;
	}

}
